﻿using Aop.Api.Domain;
using BuyWithoutLoseMoney;
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Areas.Admin.Controllers
{
    /// <summary>
    /// 新闻分类
    /// </summary>
    [Area("admin")]
    public class NewTypeController : BaseAdminController
    {
        private readonly IRepository<NewsType, int> _newTypeRepository;
        public NewTypeController(IRepository<NewsType,int> newTypeRepository, BuyWithoutLoseMoneyDBContext db) : base(db)
        {
            _newTypeRepository = newTypeRepository;
        }
        #region 新闻分类的列表，查询，新建，修改，删除
        /// <summary>
        /// 列表展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var newType=await _newTypeRepository.GetAllListAsync();
            return View(newType);
        }
        /// <summary>
        /// 分类查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Index(int page,int limit,string name)
        {
            var newType = await _newTypeRepository.GetAllListAsync();
            var query = newType.Where(x => true);
            //根据分类名搜索
            if(!string.IsNullOrEmpty(name))
            {
                query=query.Where(x => x.Name.Contains(name));
            }
            var count=query.Count();
            var list = query.OrderBy(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList()
             .Select(x => new
             {
                 id = x.Id,
                 name=x.Name,
                 createtime=x.CreateTime.ToString(),
             });
            return Json(new { Code = 0, Msg = "", Data = list, Count = count });
        }
        /// <summary>
        /// 新建
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Create()
        {
            var newtype = await _newTypeRepository.GetAllListAsync();
            ViewBag.NewType = newtype;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(NewsType newsType)
        {
            if (ModelState.IsValid)
            {
                await _newTypeRepository.InsertAsync(newsType);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，请重试！" });
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(int id)
        {
            var newType = await _newTypeRepository.GetAllListAsync();
            var newt=newType.FirstOrDefault(x=>x.Id==id);
            return View(newt);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(NewsType newsType)
        {
            if (ModelState.IsValid)
            {
                await _newTypeRepository.UpdateAsync(newsType);
                return Json(new { isSuccess = true, msg = "修改成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "修改失败，请重试！" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Del(int id)
        {
            NewsType p = new NewsType() { Id = id };
            if (ModelState.IsValid)
            {
                await _newTypeRepository.DeleteAsync(p);
                return Json(new { isSuccess = true, msg = "删除成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "删除成功，请重试！" });
        }
        #endregion
    }
}
